﻿//功用：读取配置文件值
//创建时间：2022-1-4 16:14:35
//作者：王浩力

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace WpfSnqkGasAnalysis.Model
{
    /// <summary>
    /// 读取配置文件值
    /// </summary>
    public static class MyConfigReader
    {
        /// <summary>
        /// 配置文件内容
        /// </summary>
        private static readonly string configContent;

        static MyConfigReader()
        {
            //读取自定义配置文件 sysData.ini
           
            string fileConfig = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sysData.ini");
            configContent = System.IO.File.ReadAllText(fileConfig);

            //移除#开头的所有行， 去掉注释行
            Regex regular = new Regex("^\\s*#\\s*.*", RegexOptions.Multiline);
            configContent = regular.Replace(configContent, "");

            //移除空白行
            Regex regular2 = new Regex("^\\s*", RegexOptions.Multiline);
            configContent = regular2.Replace(configContent, "");
        }

        /// <summary>
        /// 读取单行配置文件sysData.ini节点的值
        /// </summary>
        /// <param name="configName">节点名称</param>
        /// <returns>返回节点值</returns>
        public static string GetConfigValue(string configName)
        {
            //解析配置
            Regex regular3 = new Regex($"(?<={configName}).*");
            string nodeValue = regular3.Match(configContent).Value;

            //获取值移除左右空格
            return nodeValue.Trim();
        }

        /// <summary>
        /// 获取列表配置项
        /// </summary>
        /// <param name="configName">配置项名称</param>
        /// <returns>返回list字符串集合</returns>
        public static ReadOnlySpan<string> GetConfigList(string configName)
        {
            string nodeValue = String.Empty;
            try
            {
                Regex regular5 = new Regex($"(?<=({configName})\\s+\\[)(.*\\n)*(?=])");
                string txt = configContent.Substring(configContent.IndexOf(configName));
                txt = txt.Substring(0, txt.IndexOf("]") + 1);

                nodeValue = regular5.Match(txt).Value;
                nodeValue = nodeValue.Trim();
                //return nodeValue.Split("\r\n",StringSplitOptions.RemoveEmptyEntries).ToList();
                var list = nodeValue.Split(System.Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
                return new ReadOnlySpan<string>(list);
            }
            catch (Exception ex)
            {
                throw new Exception("读取数组配置异常");
            }
        }

        /// <summary>
        /// 读取似xml节点以Long_Text.开头配置里面的多行字符串
        /// </summary>
        /// <param name="configName">Long_Text.节点名称</param>
        /// <returns>返回xml之间配置的值</returns>
        public static string GetXml(string configName)
        {
            //(?<=<Long_Text.wusong>)[\s\S]*(?=</Long_Text.wusong>)
            string nodeValue = String.Empty;
            try
            {
                Regex regular5 = new Regex($"(?<=<Long_Text.{configName}>)[\\s\\S]*(?=</Long_Text.{configName}>)");
                nodeValue = regular5.Match(configContent).Value;

                return nodeValue.Trim();
            }
            catch (Exception ex)
            {
                throw new Exception("读取xml配置节点{configName}异常");
            }
        }

    }
}
